package com.project.service;

import com.project.bean.UserBean;
import com.project.dao.IUserDao;
import com.project.dao.impl.UserDaoImpl;
import com.project.server.Request;
import com.project.server.Response;
import com.project.server.Servlet;

import java.time.LocalDate;

public class AddService implements Servlet {
    private IUserDao dao = new UserDaoImpl();
    public void service(Request request, Response response){

        if ("".equals(request.getParameter("userName"))
        || "".equals(request.getParameter("pwd"))
        || "".equals(request.getParameter("birthday"))
        || "".equals(request.getParameter("phone"))){

            //调用查询所有业务方法，显示添加后的结果
            FindAllService findAll = new FindAllService();
            findAll.service(request,response);

            return;
        }

        //得到表单数据，封装成实体对象
        UserBean user = new UserBean();
        user.setUserName(request.getParameter("userName"));
        user.setPwd(request.getParameter("pwd"));
        user.setBirthday(LocalDate.parse(request.getParameter("birthday")));
        user.setPhone(request.getParameter("phone"));

        //调用持久方法，完成添加
        dao.add(user);

        //调用查询所有业务方法，显示添加后的结果
        FindAllService findAll = new FindAllService();
        findAll.service(request,response);
    }
}
